﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Workflows.Components.TextMining.Collapse
{
    public class AddressStateComparer:IAttributeComparer
    {
        private Dictionary<string, string> _StateFullName_Abbr_Mappings;

        public AddressStateComparer()
        {
            this._StateFullName_Abbr_Mappings = this.GetUSStateAbbrev();
        }

        public AttributeComparerType ComparerType
        {
            get { return AttributeComparerType.ClassComparer; }
        }

        public SqlDbType FieldDbType
        {
            get
            {
                return SqlDbType.VarChar;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public double Compare(object value1, object value2, double penalty, SqlConnection conn, string unevenDistTableName)
        {
            if (value1 == null || value1 == DBNull.Value)
                return 0;
            if (value2 == null || value2 == DBNull.Value)
                return 0;
            string st1 = value1.ToString().ToUpper();
            string st2 = value2.ToString().ToUpper();
            if (st1.Trim().Length == 0 || st2.Trim().Length == 0)
                return 0;

            if (this._StateFullName_Abbr_Mappings != null && this._StateFullName_Abbr_Mappings.ContainsKey(st1))
                st1 = this._StateFullName_Abbr_Mappings[st1];
            if (this._StateFullName_Abbr_Mappings != null && this._StateFullName_Abbr_Mappings.ContainsKey(st2))
                st2 = this._StateFullName_Abbr_Mappings[st2];
            if (st1 == st2)
                return 1;
            else
                return penalty;
        }

        public object Merge(List<object> values, SqlConnection conn, string unevenDistTableName)
        {
            object mode = null;
            int maxFreq = 0;
            Dictionary<object, int> freqs = new Dictionary<object, int>();
            foreach (object val in values)
            {
                if (val == null || val.ToString().Length == 0)
                    continue;

                if (freqs.ContainsKey(val))
                    freqs[val] = freqs[val] + 1;
                else
                    freqs.Add(val, 1);
            }
            foreach (object val in freqs.Keys)
            {
                if (freqs[val] > maxFreq)
                {
                    maxFreq = freqs[val];
                    mode = val;
                }
            }
            return mode;
        }

        private Dictionary<string, string> GetUSStateAbbrev()
        {
            Dictionary<string, string> uSStates = new Dictionary<string, string>();
            uSStates.Add("ALABAMA", "AL");
            uSStates.Add("ALASKA", "AK");
            uSStates.Add("AMERICAN SAMOA", "AS");
            uSStates.Add("ARIZONA", "AZ");
            uSStates.Add("ARKANSAS", "AR");
            uSStates.Add("CALIFORNIA", "CA");
            uSStates.Add("COLORADO", "CO");
            uSStates.Add("CONNECTICUT", "CT");
            uSStates.Add("DELAWARE", "DE");
            uSStates.Add("DISTRICT OF COLUMBIA", "DC");
            uSStates.Add("FEDERATED STATES OF MICRONESIA", "FM");
            uSStates.Add("FLORIDA", "FL");
            uSStates.Add("GEORGIA", "GA");
            uSStates.Add("GUAM", "GU");
            uSStates.Add("HAWAII", "HI");
            uSStates.Add("IDAHO", "ID");
            uSStates.Add("ILLINOIS", "IL");
            uSStates.Add("INDIANA", "IN");
            uSStates.Add("IOWA", "IA");
            uSStates.Add("KANSAS", "KS");
            uSStates.Add("KENTUCKY", "KY");
            uSStates.Add("LOUISIANA", "LA");
            uSStates.Add("MAINE", "ME");
            uSStates.Add("MARSHALL ISLANDS", "MH");
            uSStates.Add("MARYLAND", "MD");
            uSStates.Add("MASSACHUSETTS", "MA");
            uSStates.Add("MICHIGAN", "MI");
            uSStates.Add("MINNESOTA", "MN");
            uSStates.Add("MISSISSIPPI", "MS");
            uSStates.Add("MISSOURI", "MO");
            uSStates.Add("MONTANA", "MT");
            uSStates.Add("NEBRASKA", "NE");
            uSStates.Add("NEVADA", "NV");
            uSStates.Add("NEW HAMPSHIRE", "NH");
            uSStates.Add("NEW JERSEY", "NJ");
            uSStates.Add("NEW MEXICO", "NM");
            uSStates.Add("NEW YORK", "NY");
            uSStates.Add("NORTH CAROLINA", "NC");
            uSStates.Add("NORTH DAKOTA", "ND");
            uSStates.Add("NORTHERN MARIANA ISLANDS", "MP");
            uSStates.Add("OHIO", "OH");
            uSStates.Add("OKLAHOMA", "OK");
            uSStates.Add("OREGON", "OR");
            uSStates.Add("PALAU", "PW");
            uSStates.Add("PENNSYLVANIA", "PA");
            uSStates.Add("PUERTO RICO", "PR");
            uSStates.Add("RHODE ISLAND", "RI");
            uSStates.Add("SOUTH CAROLINA", "SC");
            uSStates.Add("SOUTH DAKOTA", "SD");
            uSStates.Add("TENNESSEE", "TN");
            uSStates.Add("TEXAS", "TX");
            uSStates.Add("UTAH", "UT");
            uSStates.Add("VERMONT", "VT");
            uSStates.Add("VIRGIN ISLANDS", "VI");
            uSStates.Add("VIRGINIA", "VA");
            uSStates.Add("WASHINGTON", "WA");
            uSStates.Add("WEST VIRGINIA", "WV");
            uSStates.Add("WISCONSIN", "WI");
            uSStates.Add("WYOMING", "WY");

            return uSStates;
        }

    }
}
